var gb = getApp().globalData;

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function handleForward(url, openType = 4) {
  let forwardObj = {
    url: url,
    success() {
      let t = setTimeout(function () {
        gb.isNavigating = false;
        clearTimeout(t);
      }, 1000);
    }
  };
  if (!gb.isNavigating) {
    gb.isNavigating = true;

    switch (openType) {
      case 1:   // 切换tab
        wx.switchTab(forwardObj);
        break;
      case 2:    // 重定向
        wx.redirectTo(forwardObj);
        break;
      case 3:    // 重定向
        wx.reLaunch(forwardObj);
        break;
      default:            // 正常跳转
        wx.navigateTo(forwardObj);
        break;
    }
  }
}

module.exports = {
  formatTime: formatTime,
  Forward: handleForward
}
